<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Menu_Controllers_Menu extends Gio_Core_Controller
{
	/* ############################# Frontend ############################### */
	
	/* ############################# Backend ################################ */
	
	public function addAction()
	{
		$request = $this->getRequest();
		$this->view->lang		   	  = $request->getParam('lang');
		
		$sourceId      = $request->getParam('source_id');
		$sourceMenu = (null == $sourceId) ? null : Modules_Menu_Services_Menu::getById($sourceId);
		$this->view->sourceMenu = $sourceMenu;
		
		if ($request->isPost()) {
			$user = Gio_Core_Session::getUser();

			$menuData = $request->getPost('menu');
			
			$json    = new Services_JSON();
			$items	 = ($menuData['items']) ? (array)$json->decode($menuData['items']) : array();
			
			$menuData = Modules_Menu_Services_Menu::validate($menuData);
			
			if (isset($menuData['messages_error']) && $menuData['messages_error']) {
				$this->view->errorMessages = $menuData['messages'];
				$menuData['items'] = $items;
				$this->view->menuData = $menuData;
				return;
			}
			
			$menu = array(
				'name'			=> $this->view->STRING->escape($menuData['name']),
				'description'	=> $this->view->STRING->escape($menuData['description']),
				'created_date'  => date('Y-m-d H:i:s'),
				'user_id'		=> $user['user_id'],
				'language'		=> $this->view->STRING->escape($menuData['language']),
			);
			
			$sourceItem = isset($menuData['source_item']) 
								? $menuData['source_item'] : null;
							
			$id 	 = Modules_Menu_Services_Menu::add($menu, $items, $sourceItem);
			
			Gio_Core_Messenger::getInstance()
								->addMessage($this->view->TRANSLATOR->translator('menu_add_success'));
			$this->redirect($this->view->url('menu_menu_add'));
		}
	}
	
	public function deleteAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		$menuId = $request->getPost('menu_id');
		$menu   = Modules_Menu_Services_Menu::getById($menuId);
		
		if (null == $menu){
			$this->getResponse()->setBody('RESULT_NOT_OK');
			return;	
		}
		
		/**
		 * Delete Menu
		 */
		Modules_Menu_Services_Menu::delete($menuId);
		
		$this->getResponse()->setBody('RESULT_OK');
	}
	
	public function editAction()
	{
		$request 		  = $this->getRequest();
		$this->view->lang = $request->getParam('lang');
		$menuId 		  = $request->getParam('menu_id');
		$menu		  	  = Modules_Menu_Services_Menu::getById($menuId);
		
		if (null == $menu) {
			Modules_Core_Services_Exception::error('DATA_NOT_FOUND');
		}
		
		$menuData = $menu;
		
		/**
		 * Get menu items
		 */
		$items = Modules_Menu_Services_Item::getTree($menuId);
		$menuData['items'] = $items;
		
		$this->view->menuData = $menuData;
		
		/**
		 * Get source item
		 */
		$sourceMenu = Modules_Menu_Services_Menu::getSource($menu);
		$this->view->sourceMenu = $sourceMenu;
		
		if ($request->isPost()) {
			$user = Gio_Core_Session::getUser();

			$menuData = $request->getPost('menu');
			
			$json    = new Services_JSON();
			$items	 = ($menuData['items']) ? (array)$json->decode($menuData['items']) : array();
			
			$menuData = Modules_Menu_Services_Menu::validate($menuData);
			
			if (isset($menuData['messages_error']) && $menuData['messages_error']) {
				$this->view->errorMessages = $menuData['messages'];
				$menuData['items'] = $items;
				$this->view->menuData = $menuData;
				return;
			}
			
			$menu = array(
				'menu_id'		=> $menuId,
				'name'			=> $this->view->STRING->escape($menuData['name']),
				'description'	=> $this->view->STRING->escape($menuData['description']),
				'language'		=> $this->view->STRING->escape($menuData['language']),
			);
			
			$sourceItem = isset($menuData['source_item']) 
								? $menuData['source_item'] : null;
							
			$id 	 = Modules_Menu_Services_Menu::update($menu, $items, $sourceItem);
			
			Gio_Core_Messenger::getInstance()
								->addMessage($this->view->TRANSLATOR->translator('menu_edit_success'));
			$this->redirect($this->view->url('menu_menu_edit', $menu));
		}
	}
	
	public function listAction()
	{
		$request = $this->getRequest();
		$lang    = $request->getParam('lang');
		$this->view->lang = $lang;
		$paramLang = ($lang) ? '/' . $lang : null;
		
		$perPage = 20;
		$pageIndex = (int)$request->getParam('page_index');
		if (null == $pageIndex || '' == $pageIndex || $pageIndex < 0) {
			$pageIndex = 1;
		}
		$start = ($pageIndex - 1) * $perPage;
		$this->view->pageIndex = $pageIndex;
		
		$this->view->menuDao = new Modules_Menu_Models_Mysql_Menu();
		
		$condition = array(
				'language' => $lang,
		);
		$menus = Modules_Menu_Services_Menu::find($start, $perPage, $condition);
		$numMenus = Modules_Menu_Services_Menu::count($condition);
		
		$this->view->menus = $menus; 
		
		// Pager
		require_once LIB_DIR . DS . 'PEAR' . DS . 'Pager' . DS . 'Sliding.php';
		$pagerPath = $this->view->url('menu_menu_list');
		$pagerOptions = array(
			'mode'       	=> 'Sliding',
			'append'   		=> false,
    		'perPage'    	=> $perPage,
    		'delta'      	=> 5,
			'urlVar'		=> 'page',
			'path'			=> $pagerPath,
			'fileName'		=> 'page-%d' . $paramLang,
			'separator'		=> '',
			'nextImg' 		=> '<small class="icon arrow_right"></small>',
			'prevImg'		=> '<small class="icon arrow_left"></small>',
			'altNext'		=> '',
			'altPrev'		=> '',
			'altPage'		=> '',
    		'totalItems' 	=> $numMenus,
			'currentPage'	=> $pageIndex,
			'urlSeparator'	=> '/',
			'spacesBeforeSeparator' => 0,
			'spacesAfterSeparator' => 0,
			'curPageSpanPre' => '<a href="javascript: void();" class="current">',
			'curPageSpanPost' => '</a>'
		
		);
		$pager = new Pager_Sliding($pagerOptions);
		$this->view->pager = $pager;
	}
}